﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;

namespace RabbitMQCommon.SimpleMode
{
    public class ProducerMessage
    {

        public static void SendMessage(string message)
        {
            //1.创建连接
            using (var connection = FactoryConnecation.factory.CreateConnection())
            {
                //2.创建通道
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(FactoryConnecation.Exchange, ExchangeType.Direct, durable: false, autoDelete: false, arguments: null);

                    //3.队列声明   参数:队列名 持久性:value(是持久/非持久) 是否独家的   自动删除  
                    channel.QueueDeclare(queue: FactoryConnecation.QueueName,
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

                    channel.QueueBind(FactoryConnecation.QueueName, FactoryConnecation.Exchange, FactoryConnecation.RoutKey, null);
                    //4.定义消息
                    //string message = Console.ReadLine();
                    var body = Encoding.UTF8.GetBytes(message);
                    var properites = channel.CreateBasicProperties();
                    properites.Persistent = true;
                    //5.将信息发布到交换机上
                    channel.BasicPublish(FactoryConnecation.Exchange, FactoryConnecation.RoutKey, properites, body);
                }
            }
            Console.WriteLine($"发送消息:{message}");

        }
    }
}
